<?php

namespace App\Http\Controllers;

use App\Models\Doctor;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use App\Util\Tool;

class UserController extends BaseController
{
    public function __construct()
    {

    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 退出
     */
    public function loginOut(Request $request)
    {
        $api_token = $request->api_token;
        if (empty($api_token)) {
            return $this->ResultJson(0, 'api_token不能为空!', []);
        }
        Redis::del($api_token);
        return $this->ResultJson(1, '退出成功!', []);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 医护退出
     */
    public function loginDoctorOut(Request $request)
    {
        $api_token = $request->api_token;
        if (empty($api_token)) {
            return $this->ResultJson(0, 'api_token不能为空!', []);
        }
        Redis::del($api_token);
        (new Doctor())->loginOut($api_token);
        return $this->ResultJson(1, '退出成功!', []);
    }


    /**
     * @param Request $request
     * @return mixed
     */
    public function userInfo(Request $request)
    {
        $api_token = $request->api_token;
        if (empty($api_token)) {
            return $this->ResultJson(0, 'api_token不能为空!', []);
        }
        $result = Tool::getUser($api_token);           //获取用户信息
        if (empty($result)) {
            return $this->ResultJson(3, '用户信息不存在!', []);
        }
        return $this->ResultJson(1, '获取成功!', $result);
    }

    /***
     * @param Request $request
     * 修改用户资料
     */
    public function updateUserInfo(Request $request)
    {
        $param = $request->input();
        if (isset($param['phone'])) {
            if (!$this->isValidPhoneNumber($param['phone'])) {
                return $this->ResultJson(0, '电话格式不正确!', []);
            }
            //判断短信验证码是否过期
            $code = Tool::GetSmsvalidator($param['phone']);
            if (empty($code)) {
                return $this->ResultJson(0, '短信验证码已经过期', []);
            }
            if (!isset($param['code'])) {
                return $this->ResultJson(0, '请输入验证码!', []);
            }
            //需要检查短信验证码是正确
            if (isset($param['code']) && $param['code'] != $code) {
                return $this->ResultJson(0, '短信验证码错误', []);
            }
            //更新验证码为验证过了
            $this->SetCode($param['phone'], $code);
            //短信认证设置失效时间
            Tool::UsetSmsCode($param['phone']);
        }
        if(empty($param['id'])){
            return $this->ResultJson(0, '用户ID不能为控!', []);
        }
        $res = (new User())->updateUserInfo($param);
        if ($res == true) {
            return $this->ResultJson(1, '编辑成功!', []);
        }
        return $this->ResultJson(0, '编辑失败!', []);
    }


    public function testIm(Request $request)
    {
        $res = $request->input();
        $data['im_id'] = 'user' . config("omnipay.Im.salt") . $res['user_id'];
       $result = (new Easemob())::getImUserInfo($data);
    }



    /***
     * @param Request $request
     * 注销账号
     */
    public function cancelUserInfo(Request $request)
    {
        $param = $request->input();
        if (!isset($param['user_id'])) {
            return $this->ResultJson(0, '用户ID不能为空!', []);
        }
        $res = (new User())->cancelUserInfo($param);
        if ($res == true) {
            return $this->ResultJson(1, '注销成功!', []);
        }
        return $this->ResultJson(0, '注销失败!', []);
    }


    /***
     * @param $number
     * @return false|int
     * 验证手机号
     */
    protected function isValidPhoneNumber($number)
    {
        return preg_match('/^1[3-9]\d{9}$/', $number);
    }

    protected function isValidBankCardNumber($number)
    {
        return preg_match('/([\d]{4})([\d]{4})([\d]{4})([\d]{4})([\d]{0,})?/', $number, $match);
    }


    /***
     * @param Request $request
     * 修改用户银行资料
     */
    public function updateUserBank(Request $request)
    {
        $param = $request->input();
        if (empty($param['user_id'])) {
            return $this->ResultJson(0, '用户id不能为空!', []);
        }
        if (empty($param['open_bank'])) {
            return $this->ResultJson(0, '开户行不能为空!', []);
        }
        if (empty($param['province'])) {
            return $this->ResultJson(0, '省份不能为空!', []);
        }
        if (empty($param['city'])) {
            return $this->ResultJson(0, '城市不能为空!', []);
        }
        if (empty($param['branch'])) {
            return $this->ResultJson(0, '支行不能为空!', []);
        }
        if (empty($param['card_num'])) {
            return $this->ResultJson(0, '卡号不能为空!', []);
        }
        if (empty($param['card_name'])) {
            return $this->ResultJson(0, '姓名不能为空!', []);
        }
        if (empty($param['card_type'])) {
            return $this->ResultJson(0, '类型不能为空!', []);
        }
        if (isset($param['card_num'])) {
            if (!$this->isValidBankCardNumber($param['card_num'])) {
                return $this->ResultJson(0, '卡号格式不正确!', []);
            }
        }
        $res = (new User())->updateUserBank($param);
        if ($res == true) {
            return $this->ResultJson(1, '编辑成功!', []);
        }
        return $this->ResultJson(0, '编辑失败!', []);
    }
}
